package com.ly.algorithm.offerpointer;

import java.util.Stack;

/**
 * @Classname Offer09
 * @Description
 *
 * 用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )
 *
 *  
 *
 * 示例 1：
 *
 * 输入：
 * ["CQueue","appendTail","deleteHead","deleteHead"]
 * [[],[3],[],[]]
 * 输出：[null,null,3,-1]
 * 示例 2：
 *
 * 输入：
 * ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
 * [[],[],[5],[2],[],[]]
 * 输出：[null,-1,null,null,5,2]
 *
 *
 * @Date 2021/1/26 20:53
 * @Author 冷心影翼
 */
public class Offer09 {

    public static void main(String[] args) {
        CQueue cQueue = new CQueue();
        System.out.println(cQueue.deleteHead());
        cQueue.appendTail(5);
        cQueue.appendTail(2);
        System.out.println(cQueue.deleteHead());
        cQueue.appendTail(5);
        System.out.println(cQueue.deleteHead());
    }
}


class CQueue {

    private Stack<Integer> A;
    private Stack<Integer> B;

    public CQueue() {
        A = new Stack<>();
        B = new Stack<>();
    }

    public void appendTail(int value) {
        A.add(value);
    }

    public int deleteHead() {
        if(B.isEmpty()) {
            while (!A.isEmpty()) {
                B.push(A.pop());
            }
        }
        if(!B.isEmpty()) {
            return B.pop();
        }
        return -1;
    }
}